package com.ContactsWitch.jackiez;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;
import android.util.Log;
import com.ContactsWitch.jackiez.util.LogUtil;

public class Exporter extends Activity{
    //ContentResolver cr = getContentResolver();    

	public void ExportContacts(SQLiteDatabase db,ContentResolver cr) {
        String sql_insert;
	    //ContentResolver cr = getContentResolver();    
	      //取得电话本中开始一项的光标  
	      Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
			while (cursor.moveToNext()) {
				ContactInfo cci = new ContactInfo();
				//取得联系人名字
				 int nameFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME); 
				 cci.contactName = cursor.getString(nameFieldColumnIndex);
				LogUtil.LogShow("[ContactListView]", cci.contactName, LogUtil.DEBUG);
				// 取得联系人ID  
			    int id = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts._ID));  
		        Cursor phone=cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"=?", new String[]{Integer.toString(id)}, null);//再类ContactsContract.CommonDataKinds.Phone中根据查询相应id联系人的所有电话；
				LogUtil.LogShow("[ContactListView]", Integer.toString(id), LogUtil.DEBUG);
				sql_insert = "INSERT INTO mycontacts(raw_contacts_id,my_mimetype,data) values ('"+Integer.toString(id)+"','1','"+cci.contactName+"');";
				LogUtil.LogShow("[ContactListView]", sql_insert, LogUtil.DEBUG);
		         try {
		             db.execSQL(sql_insert);
		             //setTitle("insert records ok!");
		         } catch (SQLException e) {
		             Log.e("ERROR", e.toString());
		         }
				    // 取得电话号码(可能存在多个号码)  
				    while (phone.moveToNext())  
				    {  
				        String strPhoneNumber = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
						LogUtil.LogShow("[ContactListView]", strPhoneNumber, LogUtil.DEBUG);
						sql_insert = "INSERT INTO mycontacts(raw_contacts_id,my_mimetype,data) values ('"+Integer.toString(id)+"','2','"+strPhoneNumber+"');";
				         try {
				             db.execSQL(sql_insert);
				             //setTitle("insert records ok!");
				         } catch (SQLException e) {
				             Log.e("ERROR", e.toString());
				         }
						}
				    }  
				cursor.close();	    
	}


	
}
